package com.syzg.base.module.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.stereotype.Service;

import com.syzg.base.login.model.LoginObject;
import com.syzg.base.merge.model.Module;
import com.syzg.base.merge.model.TaskTreeNode;
import com.syzg.base.module.dao.ModuleDAO;
import com.syzg.base.module.service.ModuleService;
import com.syzg.base.permission.dao.PermissionDAO;
import com.syzg.common.config.SysProperties;
import com.syzg.common.service.impl.BaseServiceImpl;
import com.syzg.common.util.SearchMap;
import com.syzg.common.util.SystemUtil;
import com.syzg.core.exception.ServiceException;

@Service("moduleService")
@RemoteProxy(name="moduleService")
public class ModuleServiceImpl extends BaseServiceImpl<Module>implements ModuleService
{

	private ModuleDAO moduleDAO;
	private PermissionDAO permissionDAO;
    
	public ModuleServiceImpl(){
    }
    
    @Resource
    public void setModuleDAO(ModuleDAO moduleDAO){
        super.setDAO(moduleDAO);
        this.moduleDAO = moduleDAO;
    }
    
    @Resource
    public void setPermissionDAO(PermissionDAO permissionDAO){
        this.permissionDAO = permissionDAO;
    }

    public List<Module> findModules() throws ServiceException {
    	SearchMap searchMap = SearchMap.forClass(Module.class);
    	searchMap.notEq("a.id", "0");
    	searchMap.addOrder("a.parentModule.id", SearchMap.ORDER_ASC);
    	searchMap.addOrder("a.orderNo", SearchMap.ORDER_ASC);
    	return  moduleDAO.findObjects(searchMap);
    }

    public String getModuleNameByModuleCode(String moduleCode) throws ServiceException{
        return moduleDAO.getModuleNameByModuleCode(moduleCode);
    }

    public String getModuleIdByModuleCode(String moduleCode) throws ServiceException{
        return moduleDAO.getModuleIdByModuleCode(moduleCode);
    }
    
    @RemoteMethod
	public List<TaskTreeNode> getModuleList(String nodeId,HttpSession session) throws ServiceException {

		if(nodeId == null){
			nodeId = "0";
		}
    	
    	LoginObject loginObject = SystemUtil.getLoginObject(session);
    	
    	String personId = loginObject.getPersonId();
    	List<TaskTreeNode> lstTaskTreeNode = new ArrayList<TaskTreeNode>();
    	List<Module> lstModule = null;
    	
    	if(personId != null && personId.equals(SysProperties.ADMIN_ID)){
    		SearchMap searchMap = SearchMap.forClass(Module.class);
    		searchMap.eq("a.parentModule.id", Long.parseLong(nodeId));
    		lstModule = moduleDAO.findObjects(searchMap);
    	} else {
        	
    		lstModule = new ArrayList<Module>();
    		
        	List<Module> lstModule2Level = permissionDAO.findModulesByPersonId(new String(personId));   	
        	
        	if(nodeId.equals("0")){
        		
        		for(Module module : lstModule2Level){
        			Module moduleParent = null;
        			for(Module moduleP : lstModule){
        				if(moduleP.getId()==module.getParentModule().getId()){
        					moduleParent = moduleP;
        					break;
        				}
        			}
        			if(moduleParent == null){
        				lstModule.add(module.getParentModule());
        			}
        		}
        	} else {
        		for(Module module : lstModule2Level){
        			if(module.getParentModule().getId().equals(nodeId)){
        				lstModule.add(module);
        			}
        		}
        	}
    	}
    	
    	if(lstModule != null){
    		for(Module module : lstModule){
    			TaskTreeNode taskTreeNode = new TaskTreeNode();
    			taskTreeNode.setId(String.valueOf(module.getId()));
    			taskTreeNode.setText(module.getName());
    			taskTreeNode.setLeaf(module.getLeafFlag()==1);
    			taskTreeNode.setUrl(module.getHref());
    			lstTaskTreeNode.add(taskTreeNode);
    		}
    	}
		
		//JSONArray jsonObjective = new JSONArray(lstTaskTreeNode);
		
		//String result = jsonObjective.toString();
		
		//result = "[" + result + "]";
		//String res = "[{text:'01',children:[{text:'01-01',leaf:true}]}]";
		return lstTaskTreeNode;
	}
	
	@RemoteMethod
	public List<Module> getParentModule() throws ServiceException {
		SearchMap searchMap = SearchMap.forClass(Module.class);
		searchMap.notEq("a.id", 0L);
		searchMap.eq("a.parentModule.id",0L);
		List<Module> lstModule = moduleDAO.findObjects(searchMap);
		for(Module module : lstModule){
			module.setParentModule(null);
			module.setModules(null);
			module.setOperations(null);
		}
		return lstModule;
	}
	
	@RemoteMethod
	public void saveModule(Module module) throws ServiceException {
		try{
			String id=module.getId();
			if(id==null){
				moduleDAO.create(module);
			} else {
				moduleDAO.update(module);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	@RemoteMethod
	public Module getModule(long id) throws ServiceException {
		try{
			Module module = (Module)moduleDAO.findObjectById(id,Module.class);
			if(module != null){
				module.setOperations(null);
				module.setModules(null);
				Module pModule = new Module();
				pModule.setId(module.getParentModule().getId());
				module.setParentModule(pModule);
			}
			return module;
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public TaskTreeNode convertToTree(List<Module> lstModule){
		TaskTreeNode res = new TaskTreeNode(
				"0","root",false,"#",null,new ArrayList<TaskTreeNode>(),"");
		LinkedHashMap<String, TaskTreeNode> map = new LinkedHashMap<String, TaskTreeNode>();
		map.put("0", res);
		for(Module module : lstModule){
			TaskTreeNode taskTreeNode = new TaskTreeNode();
			taskTreeNode.setId(String.valueOf(module.getId()));
			taskTreeNode.setText(module.getName());
			taskTreeNode.setLeaf(module.getLeafFlag()==1);
			taskTreeNode.setIconStyle(module.getIconStyle());
			taskTreeNode.setUrl(module.getHref());
			if(module.getParentModule() == null){
				taskTreeNode.setParentId("0");
			} else {
				taskTreeNode.setParentId(String.valueOf(module.getParentModule().getId()));
			}
			TaskTreeNode parentNode = map.get(taskTreeNode.getParentId());
			List<TaskTreeNode> subNodes = parentNode.getSubNodes();
			subNodes.add(taskTreeNode);
			map.put(taskTreeNode.getId(), taskTreeNode);
		}
		return res;
	}
}